/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.http;

import com.logical.allhit.setting.Proxy;
import com.logical.allhit.setting.Setting;
import com.logical.allhit.setting.SettingChangeEvent;
import com.logical.allhit.setting.SettingChangeEventListener;
import java.util.ArrayList;
import org.apache.http.HttpHost;

/**
 *
 * @author Sanjeet Jha
 */
public class ProxyManger implements SettingChangeEventListener{
    private static final ProxyManger instance = new ProxyManger();
    private ArrayList<Proxy> proxys;
    private int tempCount,currentProxyIndex;
    
    public static ProxyManger getInstance(){
        return instance;
    }
    
    private ProxyManger(){
        super();
        proxys = Setting.getInstance().getProxys();
        tempCount = 0;
        currentProxyIndex = 0;
        Setting.getInstance().addSettingChandeEventListener(this);
    }

    @Override
    public void settingChenged(SettingChangeEvent evt) {
        proxys = Setting.getInstance().getProxys();
        tempCount = 0;
        currentProxyIndex = 0;
    }
    
    public HttpHost getNextProxy(){
        HttpHost host;
        if(tempCount>=Setting.getInstance().getProxyChangCount()){
            tempCount = 0;
            currentProxyIndex++;
        }
        tempCount++;
        if(currentProxyIndex==(proxys.size())){
            currentProxyIndex = 0;
        }
        Proxy proxy = proxys.get(currentProxyIndex);
        host = new HttpHost(proxy.getName(), proxy.getPort());
        return host;
    }
}
